{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f2a9497",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: requests in e:\\anaconda3\\lib\\site-packages (2.26.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in e:\\anaconda3\\lib\\site-packages (from requests) (3.2)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\\anaconda3\\lib\\site-packages (from requests) (1.26.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in e:\\anaconda3\\lib\\site-packages (from requests) (2021.10.8)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in e:\\anaconda3\\lib\\site-packages (from requests) (2.0.4)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "798d5954",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "from Api_Key import*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b35f34cb",
   "metadata": {},
   "source": [
    "设置API Key 和API Secret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a1e9805c",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY=API_KET_FACE\n",
    "API_SECRET=API_Secret_FACE\n",
    "URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "526d94cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "IMAGE_URL =r\"C:\\Users\\yly\\Pictures\\li.jpg\"\n",
    "files = {\"image_file\": open(IMAGE_URL, \"rb\")}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "abd6d1f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请求参数\n",
    "data = {\"api_key\":API_KEY, \n",
    "        \"api_secret\":API_SECRET, \n",
    "        \"return_attributes\": \"gender,age,smiling,beauty\",\n",
    "        \"return_landmark\":1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bfb98cc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送请求\n",
    "response = requests.post(URL,data=data,files=files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e11ce3f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [200]>\n"
     ]
    }
   ],
   "source": [
    "req_con = response\n",
    "print(req_con)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13b917ca",
   "metadata": {},
   "source": [
    " API 返回了一个 JSON 格式的响应时，你可以使用 response.json() 方法来轻松地将这个 JSON 字符串转换为 Python 对象，这样你就可以更方便地在 Python 代码中处理这些数据了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "27648a6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1725980421,bde3ded1-bcce-49b5-ab49-1c94a9293ea8', 'time_used': 244, 'faces': [{'face_token': '19ee865d1ab692e397963d3cbae38ff0', 'face_rectangle': {'top': 351, 'left': 161, 'width': 761, 'height': 761}, 'landmark': {'contour_chin': {'x': 544, 'y': 1113}, 'contour_left1': {'x': 225, 'y': 506}, 'contour_left2': {'x': 219, 'y': 589}, 'contour_left3': {'x': 219, 'y': 674}, 'contour_left4': {'x': 228, 'y': 759}, 'contour_left5': {'x': 244, 'y': 843}, 'contour_left6': {'x': 274, 'y': 922}, 'contour_left7': {'x': 317, 'y': 994}, 'contour_left8': {'x': 376, 'y': 1053}, 'contour_left9': {'x': 451, 'y': 1096}, 'contour_right1': {'x': 858, 'y': 507}, 'contour_right2': {'x': 864, 'y': 590}, 'contour_right3': {'x': 864, 'y': 674}, 'contour_right4': {'x': 855, 'y': 759}, 'contour_right5': {'x': 838, 'y': 843}, 'contour_right6': {'x': 809, 'y': 922}, 'contour_right7': {'x': 766, 'y': 994}, 'contour_right8': {'x': 709, 'y': 1054}, 'contour_right9': {'x': 636, 'y': 1097}, 'left_eye_bottom': {'x': 388, 'y': 518}, 'left_eye_center': {'x': 388, 'y': 504}, 'left_eye_left_corner': {'x': 322, 'y': 508}, 'left_eye_lower_left_quarter': {'x': 354, 'y': 516}, 'left_eye_lower_right_quarter': {'x': 423, 'y': 512}, 'left_eye_pupil': {'x': 386, 'y': 499}, 'left_eye_right_corner': {'x': 455, 'y': 503}, 'left_eye_top': {'x': 387, 'y': 485}, 'left_eye_upper_left_quarter': {'x': 352, 'y': 493}, 'left_eye_upper_right_quarter': {'x': 423, 'y': 488}, 'left_eyebrow_left_corner': {'x': 266, 'y': 415}, 'left_eyebrow_lower_left_quarter': {'x': 315, 'y': 403}, 'left_eyebrow_lower_middle': {'x': 366, 'y': 396}, 'left_eyebrow_lower_right_quarter': {'x': 419, 'y': 398}, 'left_eyebrow_right_corner': {'x': 472, 'y': 399}, 'left_eyebrow_upper_left_quarter': {'x': 307, 'y': 371}, 'left_eyebrow_upper_middle': {'x': 365, 'y': 351}, 'left_eyebrow_upper_right_quarter': {'x': 430, 'y': 359}, 'mouth_left_corner': {'x': 407, 'y': 854}, 'mouth_lower_lip_bottom': {'x': 548, 'y': 927}, 'mouth_lower_lip_left_contour1': {'x': 479, 'y': 855}, 'mouth_lower_lip_left_contour2': {'x': 442, 'y': 889}, 'mouth_lower_lip_left_contour3': {'x': 488, 'y': 916}, 'mouth_lower_lip_right_contour1': {'x': 619, 'y': 857}, 'mouth_lower_lip_right_contour2': {'x': 654, 'y': 894}, 'mouth_lower_lip_right_contour3': {'x': 608, 'y': 919}, 'mouth_lower_lip_top': {'x': 549, 'y': 858}, 'mouth_right_corner': {'x': 690, 'y': 860}, 'mouth_upper_lip_bottom': {'x': 549, 'y': 855}, 'mouth_upper_lip_left_contour1': {'x': 511, 'y': 796}, 'mouth_upper_lip_left_contour2': {'x': 454, 'y': 819}, 'mouth_upper_lip_left_contour3': {'x': 479, 'y': 852}, 'mouth_upper_lip_right_contour1': {'x': 588, 'y': 796}, 'mouth_upper_lip_right_contour2': {'x': 644, 'y': 822}, 'mouth_upper_lip_right_contour3': {'x': 618, 'y': 854}, 'mouth_upper_lip_top': {'x': 550, 'y': 806}, 'nose_contour_left1': {'x': 494, 'y': 503}, 'nose_contour_left2': {'x': 470, 'y': 619}, 'nose_contour_left3': {'x': 497, 'y': 701}, 'nose_contour_lower_middle': {'x': 555, 'y': 712}, 'nose_contour_right1': {'x': 614, 'y': 505}, 'nose_contour_right2': {'x': 641, 'y': 621}, 'nose_contour_right3': {'x': 611, 'y': 703}, 'nose_left': {'x': 444, 'y': 681}, 'nose_right': {'x': 663, 'y': 685}, 'nose_tip': {'x': 559, 'y': 621}, 'right_eye_bottom': {'x': 712, 'y': 520}, 'right_eye_center': {'x': 712, 'y': 504}, 'right_eye_left_corner': {'x': 643, 'y': 505}, 'right_eye_lower_left_quarter': {'x': 675, 'y': 514}, 'right_eye_lower_right_quarter': {'x': 748, 'y': 517}, 'right_eye_pupil': {'x': 711, 'y': 500}, 'right_eye_right_corner': {'x': 780, 'y': 509}, 'right_eye_top': {'x': 712, 'y': 482}, 'right_eye_upper_left_quarter': {'x': 674, 'y': 486}, 'right_eye_upper_right_quarter': {'x': 749, 'y': 491}, 'right_eyebrow_left_corner': {'x': 630, 'y': 396}, 'right_eyebrow_lower_left_quarter': {'x': 681, 'y': 399}, 'right_eyebrow_lower_middle': {'x': 733, 'y': 396}, 'right_eyebrow_lower_right_quarter': {'x': 782, 'y': 401}, 'right_eyebrow_right_corner': {'x': 831, 'y': 407}, 'right_eyebrow_upper_left_quarter': {'x': 673, 'y': 359}, 'right_eyebrow_upper_middle': {'x': 733, 'y': 352}, 'right_eyebrow_upper_right_quarter': {'x': 790, 'y': 366}}, 'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 24}, 'smile': {'value': 0.093, 'threshold': 50.0}, 'beauty': {'male_score': 47.548, 'female_score': 53.445}}}], 'image_id': 'IaEk76FIsteaAvmJ0vlLbw==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "req_con = response.json()\n",
    "print(req_con)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433bbed4",
   "metadata": {},
   "source": [
    "用“items”迭代处理字典；用“in”检查成员关系。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e9ff8a04",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取人脸坐标信息\n",
    "faces = req_con.get('faces', [])\n",
    "face_coordinates = []\n",
    "\n",
    "for face in faces:\n",
    "   \n",
    "    face_info = {\n",
    "        'face_token': face.get('face_token'),\n",
    "        'landmark': face.get('landmark'),\n",
    "        'attributes':face.get('attributes')\n",
    "    }\n",
    "  \n",
    "    face_coordinates.append(face_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "cb6f8876",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存人脸坐标字典到文件\n",
    "output_file = 'face_coordinates.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "07586a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(output_file, 'w') as f:\n",
    "    json.dump(face_coordinates, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c26ebafb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d7e0953",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
